package com.ubao.weibo;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.ubao.R;
import com.ubao.activity.MyApp.GoBack;
import com.ubao.base.RequestBaseActivity;
import com.ubao.global.GoBackable;
import com.ubao.network.ResponseInterface;
import com.ubao.ui.titlebar.GTitleBar;


public class ShareWeiboActivity extends RequestBaseActivity
		implements
			GoBackable {
	protected EditText mEditText;
	protected TextView mTextNum;
	protected final int WEIBO_MAX_LENGTH = 120;
	protected String mContent;
	protected String mUrl;
	protected TextView mTextBind;
	protected GTitleBar gTB;

	public void onCreate(Bundle bundle) {
		setContentView(R.layout.ui_share_weibo);
		mEditText = (EditText) findViewById(R.id.edtContent);
		mTextNum = (TextView) findViewById(R.id.maxNumberWord);
		mTextBind = (TextView) findViewById(R.id.isWeiboBing);
		gTB = (GTitleBar) findViewById(R.id.share_weibo_title);

		initBundle();

		mEditText.setText(mContent);
		setRemainWord();
		mEditText.addTextChangedListener(new TextWatcher() {
			@Override
			public void afterTextChanged(Editable s) {

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
			}

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				setRemainWord();
			}

		});
		super.onCreate(bundle);
	}

	private void initBundle() {
		Intent intent = getIntent();
		mContent = intent.getStringExtra("mContent");
		mUrl =  intent.getStringExtra("mUrl");
	}

	private void setRemainWord() {
		String mText = mEditText.getText().toString();
		int len = mText.length();
		double tempLen = 0;
		char[] temp = mText.toCharArray();
		for (int i = 0; i < temp.length; i++) {
			int c = (int) temp[i];
			if (19968 <= c && c < 40623)
				tempLen += 1;
			else
				tempLen += 0.5;
		}

		len = (int) tempLen;
		if (len <= WEIBO_MAX_LENGTH) {
			len = WEIBO_MAX_LENGTH - len;
			mTextNum.setText("还可输入" + String.valueOf(len) + "字");
			mTextNum.setTextColor(Color.WHITE);
		} else {
			len = len - WEIBO_MAX_LENGTH;
			mTextNum.setTextColor(Color.RED);
			mTextNum.setText("已超出" + String.valueOf(len) + "字");
		}
	}

	@Override
	protected void httpCompleted(ResponseInterface resp) {
		// TODO Auto-generated method stub
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void getIntentData() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void setIntentData() {
		// TODO Auto-generated method stub

	}

	public void setGoBack() {
		Bundle gobackBundle = new Bundle();
		gobackBundle.putString("back2", this.getClass().getName());
		GoBack.goBackStack.push(gobackBundle);
	}
	protected Handler toastHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			Toast.makeText(ShareWeiboActivity.this, (String) msg.obj,
					Toast.LENGTH_LONG).show();
			super.handleMessage(msg);
		}
	};

	
	protected String getSendContent(){
		return mEditText.getText().toString().trim()+mUrl;
	}
}
